#!/bin/bash

echo -e "\n================================================================
Cream Framework v1.2 - Initialization
----------------------------------------------------------------
Author            : Maelstrom <devcreamframework@gmail.com>
Public repository : http://code.google.com/p/cream-cms-repository/
================================================================\n"

mysqlfile='_SQL/base.sql'
tempfile='mysql.tmp'
localconfigfile='core/config/config.local.ini'
prodconfigfile='core/config/config.ini'
config=('' '' '' '')

# MYSQL HOST
while [ -z $mysqlHost ]
do
    read -p "MySql host     : " mysqlHost
done
config[0]=$mysqlHost

# MYSQL USER
while [ -z $mysqlUser ]
do
    read -p "MySql user     : " mysqlUser
done
config[1]=$mysqlUser

# MYSQL PASSWORD
while [ -z $mysqlPasswd ]
do
    read -p "MySql password : " mysqlPasswd
done
config[2]=$mysqlPasswd

# MYSQL DATABASE
while [ -z $mysqlDatabase ]
do
    read -p "MySql database : " mysqlDatabase
done
config[3]=$mysqlDatabase

echo -e "\nPending..."

mysqlcreate="CREATE DATABASE ${config[3]} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;"
echo $mysqlcreate>$tempfile

mysql -h ${config[0]} -u ${config[1]} -p${config[2]} < $tempfile
mysql -h ${config[0]} -u ${config[1]} -p${config[2]} ${config[3]} < $mysqlfile

rm $tempfile

# BUILD CONFIG FILES
echo -e "[mysql]
host     = ${config[0]}
login    = ${config[1]}
password = ${config[2]}
base     = ${config[3]}">$localconfigfile
echo -e "[mysql]
host     = ${config[0]}
login    = ${config[1]}
password = ${config[2]}
base     = ${config[3]}">$prodconfigfile

# CHMOD
chmod 777 cache
chmod -R 777 dynamics
chmod 777 sessions
chmod 777 backend/debug
chmod 777 backend/temp
chmod 777 backend/logs

echo -e "Configuration complete !\n"